INCLUDE(ExternalProject)

SET(KRB_SOURCES_DIR ${THIRD_PARTY_PATH}/krb5-1.14.6/src)
SET(KRB_INSTALL_DIR ${KRB_SOURCES_DIR}/output)
SET(KRB_INCLUDE_DIR "${KRB_INSTALL_DIR}/include" CACHE PATH "krb include directory." FORCE)

IF(APPLE)
    SET(EXTERNAL_OPENSSL_INCLUDE "/usr/local/opt/openssl/include")
    SET(EXTERNAL_OPENSSL_LIB "/usr/local/opt/openssl/lib")
    # krb cannot be built statically on mac directly as there's a duplicate symbols in dbutil and.
    # So, we have to build krb dynamically, then build statically.
    SET(CONFIG_CMD cd ${KRB_SOURCES_DIR} && autoreconf &&
            CPPFLAGS=-I${EXTERNAL_OPENSSL_INCLUDE} CFLAGS=-fPIC CXXFLAGS=-fPIC LDFLAGS=-L${EXTERNAL_OPENSSL_LIB} ./configure --prefix=${KRB_INSTALL_DIR} &&
            make -j 16 && make install &&
            CPPFLAGS=-I${EXTERNAL_OPENSSL_INCLUDE} CFLAGS=-fPIC CXXFLAGS=-fPIC LDFLAGS=-L${EXTERNAL_OPENSSL_LIB} ./configure --prefix=${KRB_INSTALL_DIR} --enable-static --disable-shared )
ELSE()
    SET(CONFIG_CMD cd ${KRB_SOURCES_DIR} && CPPFLAGS=-fPIC ./configure --prefix=${KRB_INSTALL_DIR} --enable-static --disable-shared)
ENDIF()

ExternalProject_Add(
    extern_krb
    DOWNLOAD_DIR ${THIRD_PARTY_PATH}
    DOWNLOAD_COMMAND rm -rf ${KRB_SOURCES_DIR} && wget http://web.mit.edu/kerberos/dist/krb5/1.14/krb5-1.14.6.tar.gz -O krb5-1.14.6.tar.gz && tar xzf krb5-1.14.6.tar.gz
    CONFIGURE_COMMAND ${CONFIG_CMD}
    BUILD_COMMAND cd ${KRB_SOURCES_DIR} && make -j 16
    INSTALL_COMMAND cd ${KRB_SOURCES_DIR} && make install
)

SET(KRB_LIBRARIES_DIR "${KRB_INSTALL_DIR}/lib" CACHE FILEPATH "KRB_LIBRARIES" FORCE)
ADD_LIBRARY(krb5 STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET krb5 PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkrb5.a)

ADD_LIBRARY(ssapi_krb5 STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET ssapi_krb5 PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libgssapi_krb5.a)

ADD_LIBRARY(k5crypto STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET k5crypto PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libk5crypto.a)

ADD_LIBRARY(kadm5clint STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET kadm5clint PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkadm5clnt_mit.a)

ADD_LIBRARY(kadm5srv STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET kadm5srv PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkadm5srv_mit.a)

ADD_LIBRARY(krad STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET krad PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkrad.a)

ADD_LIBRARY(krb5_db2 STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET krb5_db2 PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkrb5_db2.a)

ADD_LIBRARY(krb5_otp STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET krb5_otp PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkrb5_otp.a)

ADD_LIBRARY(krb5support STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET krb5support PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkrb5support.a)

ADD_LIBRARY(com_err STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET com_err PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libcom_err.a)

ADD_LIBRARY(gssrpc STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET gssrpc PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libgssrpc.a)

ADD_LIBRARY(kdb5 STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET kdb5 PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkdb5.a)

ADD_LIBRARY(krb5_k5tls STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET krb5_k5tls PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkrb5_k5tls.a)

ADD_LIBRARY(krb5_pkinit STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET krb5_pkinit PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkrb5_pkinit.a)

ADD_LIBRARY(krb5_test STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET krb5_test PROPERTY IMPORTED_LOCATION ${KRB_LIBRARIES_DIR}/libkrb5_test.a)

merge_static_libs(krb krb5_db2 krb5_otp krb5_k5tls krb5_pkinit krb5_test kadm5clint kadm5srv kdb5 gssrpc ssapi_krb5 krad krb5 com_err k5crypto krb5support)

ADD_DEPENDENCIES(krb extern_krb)
#target_link_libraries(krb -lkeyutils)

INCLUDE_DIRECTORIES(${KRB_INCLUDE_DIR})

LIST(APPEND external_project_dependencies krb)

